﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Interface;

namespace mogco
{
    public static class Logger
    {
        public static TxtLogger txtLogger
        {
            get;
            set;
        }

        static Logger()
        {
            txtLogger = new TxtLogger();
        }

        public static void Initialize()
        {
            Logger.txtLogger.WriteMessage("Initialize");
        }

        public class TxtLogger : IPLogger
        {
            private StreamWriter logWriter;

            public TxtLogger()
            {
            }

            private StreamWriter Writer
            {
                get
                {
                    if (logWriter == null)
                        logWriter = new StreamWriter(Environment.TickCount.ToString() + ".txt");
                    return logWriter;
                }
            }

            public void WriteMessage(string message)
            {
                // TODO: finish
                try
                {
                    DateTime cut_date = DateTime.Now;
                    StackTrace stackTrace = new StackTrace();
                    string time_line = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
                    string call_methods = stackTrace.GetFrame(2).GetMethod().Name + ".";
                    call_methods += stackTrace.GetFrame(1).GetMethod().Name;
                    string new_line = call_methods;
                    Writer.WriteLine(time_line + " " + new_line + ": " + message);
                    Writer.Flush();
                }
                catch (Exception e)
                {
                    // О плохая ошибка, надо сделать рекурсию!
                    logWriter = null;
                    this.WriteMessage(message);
                }

            }

            #region IPLogger Members

            public void PWriteMessage(string message)
            {
                WriteMessage(message);
            }

            #endregion
        }
    }
}
